﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kugar;

namespace Kugar.UI.WinForm
{
    /// <summary>
    ///     计算指定DataGridView指定列的平均值
    /// </summary>
    public class TotalColumnRow_Avg : TotalColumnRowBase
    {
        protected override object GetColumnTotalValue(DataGridViewColumn column)
        {
            if (!labelMappingCache.ContainsKey(column))
            {
                return null;
            }

            CalcCellValue calcDelgte = null;

            if (column.ValueType == typeof(int))
            {
                calcDelgte = Int32ColumnSum;
            }
            else if (column.ValueType == typeof(short))
            {
                calcDelgte = Int16ColumnSum;
            }
            else if (column.ValueType == typeof(long))
            {
                calcDelgte = Int64ColumnSum;
            }
            else if (column.ValueType == typeof(ushort))
            {
                calcDelgte = UInt16ColumnSum;
            }
            else if (column.ValueType == typeof(uint))
            {
                calcDelgte = UInt32ColumnSum;
            }
            else if (column.ValueType == typeof(ulong))
            {
                calcDelgte = UInt64ColumnSum;
            }
            else if (column.ValueType == typeof(double))
            {
                calcDelgte = DoubleColumnSum;
            }
            else if (column.ValueType == typeof(float))
            {
                calcDelgte = SingleColumnSum;
            }
            else if (column.ValueType == typeof(short))
            {
                calcDelgte = ShortColumnSum;
            }
            else
            {
                calcDelgte = DecimalColumnSum;
            }

            if (calcDelgte==null)
            {
                return null;
            }

            var rowCount = GridSource.Rows.Count;

            if (rowCount<=0)
            {
                return 0;
            }

            var colIndex = column.Index;

            object result = 0;


            for (int i = 0; i < rowCount; i++)
            {
                var value = GridSource.Rows[i].Cells[colIndex].Value;

                if (value == null)
                {
                    continue;
                }

                calcDelgte(ref result, value);
            }


            return (decimal)result/rowCount;
        }

        private static void Int16ColumnSum(ref object lastValue, object value)
        {
            if (value != null)
            {
                lastValue = (short)lastValue + (short)value;
            }
        }

        private static void Int32ColumnSum(ref object lastValue, object value)
        {
            if (value != null)
            {
                lastValue = (int)lastValue + (int)value;
            }
        }

        private static void Int64ColumnSum(ref object lastValue, object value)
        {
            if (value != null)
            {
                lastValue = (long)lastValue + (long)value;
            }
        }

        private static void UInt16ColumnSum(ref object lastValue, object value)
        {
            if (value != null)
            {
                lastValue = (ushort)lastValue + (ushort)value;
            }
        }

        private static void UInt32ColumnSum(ref object lastValue, object value)
        {
            if (value != null)
            {
                lastValue = (uint)lastValue + (uint)value;
            }
        }

        private static void UInt64ColumnSum(ref object lastValue, object value)
        {
            if (value != null)
            {
                lastValue = (ulong)lastValue + (ulong)value;
            }
        }

        private static void DoubleColumnSum(ref object lastValue, object value)
        {
            if (value != null)
            {
                lastValue = (double)lastValue + (double)value;
            }
        }

        private static void DecimalColumnSum(ref object lastValue, object value)
        {
            if (value != null)
            {
                try
                {
                    lastValue = Convert.ToDecimal(lastValue) + (decimal) value;
                }
                catch (Exception)
                {
                    
                }
                
            }
        }

        private static void SingleColumnSum(ref object lastValue, object value)
        {
            if (value != null)
            {
                lastValue = (float)lastValue + (float)value;
            }
        }

        private static void ShortColumnSum(ref object lastValue, object value)
        {
            if (value != null)
            {
                lastValue = (short)lastValue + (short)value;
            }
        }

        protected delegate void CalcCellValue(ref object lastValue, object value);
    }
}
